package com.difsonapps.android.marker.activities;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import com.difsonapps.android.marker.R;
import com.difsonapps.android.marker.R.layout;
import com.difsonapps.android.marker.R.menu;

import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class ConfigurationActivity extends Activity {
	private final int SELECT_PHOTO = 1;
	String currentUploadeFile = null;
	
	File currentSelectedFile=null;
	View[] stepsLayout = new View[3];
	int currentStep = 0;
	ImageView[] imagesViewsNav = new ImageView[3];
	TextView[] textsViewsNav = new TextView[3];
	int[] imagesRessources = new int[]{R.drawable.ic1, R.drawable.ic2, R.drawable.ic3};
	int[] imagesRessourcesSelected = new int[]{R.drawable.ics1, R.drawable.ics2, R.drawable.ics3};
	
	View loadingLayout;
	View contentLayout;
	TextView loadingStatusMessage;
	
	Button continueButton;
	Button cancelButton;
	Button chooseButton;
	
	private Spinner filterSpinner;
	int filterLevelSelected = -1;
	
	EditText occupationEditText;
	EditText aboutmeEditText;
	
	ImageView selectedImageView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_configuration);
		
		imagesViewsNav[0] = (ImageView)findViewById(R.id.navigationImage1);
		textsViewsNav[0] = (TextView)findViewById(R.id.navigationText1);
		imagesViewsNav[1] = (ImageView)findViewById(R.id.navigationImage2);
		textsViewsNav[1] = (TextView)findViewById(R.id.navigationText2);
		imagesViewsNav[2] = (ImageView)findViewById(R.id.navigationImage3);
		textsViewsNav[2] = (TextView)findViewById(R.id.navigationText3);
		
		stepsLayout[0] = findViewById(R.id.step1Layout);
		stepsLayout[1] = findViewById(R.id.step2Layout);
		stepsLayout[2] = findViewById(R.id.step3Layout);
		
		loadingLayout = findViewById(R.id.loading_status);
		contentLayout = findViewById(R.id.contentLayout);
		loadingStatusMessage = (TextView)findViewById(R.id.loading_status_message);
		occupationEditText = (EditText)findViewById(R.id.occupationEditText);
		aboutmeEditText = (EditText)findViewById(R.id.aboutmeEditText);
		
		selectedImageView = (ImageView)findViewById(R.id.selectedImageView);
		
		filterSpinner = (Spinner)findViewById(R.id.filterSpinner);
		List<String> list = new ArrayList<String>();
        list.add("Me only");
        list.add("My friends");
        list.add("Friends of friends");
        list.add("Public");
         
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,list);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        filterSpinner.setAdapter(dataAdapter);
		filterSpinner.setOnItemSelectedListener(new CustomOnItemSelectedListener());
		
		continueButton = (Button)findViewById(R.id.continueButton);
		cancelButton = (Button)findViewById(R.id.cancelButton);
		chooseButton = (Button)findViewById(R.id.chooseButton);
		
		chooseButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				toast("Lunching pick image");
				Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
				photoPickerIntent.setType("image/*");
				startActivityForResult(photoPickerIntent, SELECT_PHOTO);
			}
		});
		
		continueButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				saveAndNextStep();
			}
		});
		cancelButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				nextStep();
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.configuration, menu);
		return true;
	}
	
	 
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 
 
        switch(requestCode) { 
        case SELECT_PHOTO:
            if(resultCode == RESULT_OK){
				try {
					//showProgress();
					final Uri imageUri = imageReturnedIntent.getData();
					toast(imageUri.getPath());
					toast(getPath(imageUri));
					File file = new File(getPath(imageUri));
					if(file.exists()) currentSelectedFile = file;
					final InputStream imageStream = getContentResolver().openInputStream(imageUri);
					final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
					selectedImageView.setImageBitmap(selectedImage);
					//hideProgress();
					//upload(); TODO
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				}
 
            }
        }
    }
    
    public String getPath(Uri uri) {
    	String[] projection = { MediaStore.Images.Media.DATA };
    	Cursor cursor = managedQuery(uri, projection, null, null, null);
    	startManagingCursor(cursor);
    	int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    	cursor.moveToFirst();
    	return cursor.getString(column_index);
    }
	
	private void saveAndNextStep(){
		//TODO Save datas of the current step if provided
		nextStep();
	}
	
	private void nextStep(){
		if(currentStep<2){
			stepsLayout[currentStep].setVisibility(View.GONE);
			textsViewsNav[currentStep].setVisibility(View.GONE);
			imagesViewsNav[currentStep].setImageResource(imagesRessources[currentStep]);
			
			currentStep++;
			stepsLayout[currentStep].setVisibility(View.VISIBLE);
			textsViewsNav[currentStep].setVisibility(View.VISIBLE);
			imagesViewsNav[currentStep].setImageResource(imagesRessourcesSelected[currentStep]);
			
			if(currentStep==2){
				continueButton.setText(getResources().getString(R.string.save));
			}
			
		}else{
			//Show the main activity
		}
	}
	
	public void toast(String text){
		Toast.makeText(this, text,Toast.LENGTH_LONG).show();
	}
	
	 class CustomOnItemSelectedListener implements OnItemSelectedListener {
	 
	    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
	    	filterLevelSelected = pos;
	    	//toast("Level : "+pos);
	    	//toast("On Item Select : \n" + parent.getItemAtPosition(pos).toString()) ;
	        //Toast.makeText(parent.getContext(), "On Item Select : \n" + parent.getItemAtPosition(pos).toString(),Toast.LENGTH_LONG).show();
	    }
	 
	    @Override
	    public void onNothingSelected(AdapterView<?> arg0) {
	        // TODO Auto-generated method stub
	 
	    }
	 
	}

}
